package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.CollectAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.CollectData;
import com.xywy.bloodandroid.bean.User;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.MD5;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;
import com.xywy.bloodandroid.vollery.PostRequest;
import com.xywy.bloodandroid.vollery.VolleyManager;

/**
 * 我的收藏
 */
public class MyCollectActivity extends BaseActivity {

	protected static final int FAIL = 0;
	protected static final int SUCCESS = 1;
	private ListView lv;
	private CollectAdapter adapter;
	private TextView middle_title;
	private RelativeLayout left_img;
	private List<CollectData> datas; // 获取的数据

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_collect);
		lv = (ListView) findViewById(R.id.lv);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("我的收藏");
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		datas = new ArrayList<CollectData>();

	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				MyCollectActivity.this.finish();
			}
		});
	}

	// 获取数据
	@Override
	protected void initData() {
		String url = "http://api.yun.xywy.com/index.php/app/collect/list_data/"
				+ "111" + "&userid=" + User.getInstance(context).getUserid()
				+ "&app_id=" + "2";

		PostRequest<String> postRequest = new PostRequest<String>(url,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						Message msg = Message.obtain();
						msg.what = FAIL;
						handler.sendMessage(msg);

					}

					@SuppressWarnings("unchecked")
					@Override
					public void onSuccess(String response) {

						try {
							JSONObject jsonObject = new JSONObject(response);
							int state = jsonObject.getInt("state");
							if (state == 200) {

								datas = (List<CollectData>) JsonUtil.parseJsonToList(
										jsonObject.getString("data"), //
										new TypeToken<List<CollectData>>() {
										}.getType());

								Message msg = Message.obtain();
								msg.what = SUCCESS;
								msg.obj = datas;
								handler.sendMessage(msg);
							}

						} catch (JSONException e) {
							Message msg = Message.obtain();
							msg.what = FAIL;
							handler.sendMessage(msg);
							e.printStackTrace();
						}

						showToast("成功连接");
					}
				});

		String sign = null;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.tag);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("sign", sign);
		params.put("tag", Constants.tag);
		params.put("xywy_userid", User.getInstance(context).getUserid());
		params.put("app_id", "2");
		postRequest.setParams(params);
		postRequest.setIsParseJson(false);
		executeRequest(postRequest);
		// VolleyManager.addRequest(postRequest, null);

	}

	// 展示数据
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				UIUtil.DissDialog(context, waittingDialog);
				@SuppressWarnings("unchecked")
				List<CollectData> data = (List<CollectData>) msg.obj;
				if (data == null || data.size() < 1) {
					showToast("还没有收藏过文章。");
				}
				adapter = new CollectAdapter(MyCollectActivity.this, data);
				lv.setAdapter(adapter);
				break;

			case FAIL:
				UIUtil.DissDialog(context, waittingDialog);
				showToast("获取失败");
				break;
			}
		};

	};
}
